// ==UserScript==
// @name         Micoua_迷你浏览器
// @version      1.0.0
// @author       micoua
// @namespace    https://greasyfork.org/zh-CN/users/162781
// @description  迷你浏览器

// @include      *

//               ↓ jQuery核心文件 ↓
// @require      http://libs.baidu.com/jquery/2.1.4/jquery.min.js

// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// ==/UserScript==

(function () {
    /** 是否最外 */
    if (self != top) { return; }

    /** 初始化JQ语法 */
    var $ = $ || window.$;

    /** 定义变量 */
    var curWebURL = window.location.href;
    var mainBtnIcon = "https://gitee.com/micoua/public-resources/raw/master/picture/icon/browser-200x200.png";
    var blLiveSignPath = "https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign";
    var urlList = new Array();
    urlList[0] = "https://h5.ele.me/order/";
    var urlIndex = 0;
    var eleIsLoad = false;

    /** 基础入口 */
    function main() {
        // 添加元素
        addElement();
        // 加载样式
        loadStyles();
        // B站直播签到
        //console.log("[" + getFormatDate() + "]: ");
        //blLiveSign();
    }

    /** B站直播签到 */
    var blLiveSign = function () {
        $("body").append(`<iframe id="micoua-bl-iframe"></iframe>`);
        $("#micoua-bl-iframe").attr("src", blLiveSignPath);
        console.log("[" + getFormatDate() + "]: " + $("#micoua-bl-iframe").text());
    }

    /** 获取当前时间 */
    var getFormatDate = function () {
        var nowDate = new Date();
        var year = nowDate.getFullYear();
        var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
        var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
        var hour = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours();
        var minute = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
        var second = nowDate.getSeconds() < 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
        return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    }

    /** 加载样式 */
    var loadStyles = function (selector) {
        // 添加按钮样式
        var btnStyles = `<style>
            #micoua-float-btn {
                position: fixed;
                z-index: 999999999;
                top: 30px;
                right: -25px;
                width: 40px;
                height: 40px;
                border: 2px solid #ff516a;
                border-radius: 40px;
                opacity: 0.5;
                cursor: pointer;
                background: url(` + mainBtnIcon + `) center no-repeat;
                background-size: 90%;
                transition: all .3s;
            }
            #micoua-float-btn:hover {
                transition: all .3s;
                right: 5px;
                opacity: 1;
            }
        </style>`;
        $("head").append(btnStyles);

        // 添加内嵌页面样式
        var iframeStyles = `<style>
            #micoua-float-iframe {
                position: fixed;
                z-index: 999999999;
                right: -300px;
                width: 285px;
                height: 550px;
                border-radius: 5px;
                box-shadow: 8px 10px 15px #af6363;
                transform: scale(1);
                transition: all .2s;
                opacity: .5;
            }
            #micoua-float-iframe:hover {
                opacity: 1;
            }
            #micoua-httpInput {
                width: 100%;
                height: 5%;
                border-radius: 5px;
                border: 2px solid #ff516a;
                background: white;
            }
            #micoua-httpInput .micoua-control {
                position: relative;
                float: left;
                padding: 0 4px;
                height: 28px;
                border: none;
                cursor: pointer;
                line-height: 28px;
                font-size: 13px;
            }
            #micoua-httpInput .micoua-control:hover {
                background: #e4e0e0;
            }
            #micoua-httpInput input {
                position: relative;
                float: left;
                width: 78%;
                height: 100%;
                padding: 0 10px;
                box-shadow: 0px 0px 5px #ff516a inset;
                border: none;
            }
            #micoua-iframe {
                width: 100%;
                height: 95%;
                border-radius: 5px;
                border: 2px solid #ff516a;
            }
        </style>`;
        $("head").append(iframeStyles);
    }

    /** 添加元素 */
    var addElement = function () {
        $("body").append(`<div id="micoua-float-btn"></div>`);
        $("body").append(`<div id="micoua-float-iframe"></div>`);
        $("#micoua-float-iframe").append(`<div id="micoua-httpInput"></div>`);
        $("#micoua-httpInput").append(`<div id="micoua-http-back" class="micoua-control">〈</div>`);
        $("#micoua-httpInput").append(`<div id="micoua-http-advance" class="micoua-control">〉</div>`);
        $("#micoua-httpInput").append(`<input type="url"/>`);
        $("#micoua-float-iframe").append(`<iframe id="micoua-iframe"></iframe>`);

        // 点击按钮
        $("#micoua-float-btn").on("click", function () {
            // 加载页面
            if (!eleIsLoad) {
                eleIsLoad = true;
                $("#micoua-float-iframe iframe").attr("src", urlList[urlIndex]);
            }

            // 更新内嵌页面
            var btnTop = parseInt($("#micoua-float-btn").css("top"));
            var iframeRight = parseInt($("#micoua-float-iframe").css("right"));
            iframeRight = iframeRight < 0 ? 10 : -parseInt($("#micoua-float-iframe").css("width")) - 10;
            $("#micoua-float-iframe").css({
                "top": btnTop + 50 + "px",
                "right": iframeRight + "px"
            });
            $("#micoua-httpInput input").val(urlList[urlIndex]);
        });

        // 地址框回车
        $("#micoua-httpInput input").on("keypress", function (e) {
            var keyCode = null;
            if (e.which) {
                keyCode = e.which;
            } else if (e.keyCode) {
                keyCode = e.keyCode;
            }
            if (keyCode === 13) {
                var urlText = this.value + "";
                if (!(urlText.startsWith("https://") || urlText.startsWith("http://"))) {
                    urlText = "http://" + urlText;
                }
                urlIndex++;
                urlList[urlIndex] = urlText;
                $("#micoua-float-iframe iframe").attr("src", urlList[urlIndex]);
                $("#micoua-httpInput input").val(urlList[urlIndex]);
            }
        });

        // 页面后退
        $("#micoua-http-back").on("click", function (e) {
            if (urlIndex > 0) {
                $("#micoua-float-iframe iframe").attr("src", urlList[--urlIndex]);
                $("#micoua-httpInput input").val(urlList[urlIndex]);
            }
        });

        // 页面前进
        $("#micoua-http-advance").on("click", function (e) {
            if (urlIndex < urlList.length - 1) {
                $("#micoua-float-iframe iframe").attr("src", urlList[++urlIndex]);
                $("#micoua-httpInput input").val(urlList[urlIndex]);
            }
        });
    }

    /** 加载完所有数据后进入主函数 */
    if (true) main();
})();